package com.example.controller.sys;
import com.example.pojo.Dict;
import com.example.service.IDictService;
import com.example.util.DictUtils;
import com.example.vo.ResultVo;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import io.swagger.annotations.Api;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.sql.Date;
import java.util.Arrays;

/**
 * <p>
 * 字典表 前端控制器
 * </p>
 *
 * @author wsl
 * @since 2022-04-01
 */
@RestController
@RequestMapping("/sys/dict")
@Api(tags = "数据字典管理",value = "")
public class DictController {

    @Autowired
    private IDictService dictService;

    @ApiOperation(value = "查询字典")
    @GetMapping("/query")
    public ResultVo getDictList(
            String name,
            String code,
            Integer enabled,
            Date start,
            Date end
    ){
        return dictService.getDictList(name,code,enabled,start,end);
    }

    @ApiOperation(value = "添加字典目录")
    @PostMapping("/add")
    public ResultVo addDict(@RequestBody Dict dict){
        return dictService.addDict(dict);
    }

    @ApiOperation(value = "添加字典子项")
    @PostMapping("/add-children")
    public ResultVo addChildren(@RequestBody Dict dict){
        return dictService.addChildren(dict);
    }

    @ApiOperation(value = "删除字典")
    @DeleteMapping("/delete")
    public ResultVo deleteDict(Integer[] ids){
        return dictService.deleteDict(ids);
    }

    @ApiOperation(value = "修改字典")
    @PutMapping("/update")
    public ResultVo updateDict(@RequestBody Dict dict){
        if(dictService.updateById(dict)){
            return ResultVo.ok("修改成功");
        }
        return ResultVo.error("修改失败");
    }

    @ApiOperation(value = "导出字典表数据")
    @GetMapping("/export")
    public void exportDict(HttpServletResponse response){
        dictService.exportDict(response);
    }

    @ApiOperation(value = "导入字典表数据")
    @PostMapping("/import")
    public void importDict(MultipartFile file){
        dictService.importDict(file);
    }

    @ApiOperation(value = "获取字典类型选项")
    @GetMapping("/types")
    public ResultVo getDictTypeOptions(){
        return dictService.getDictTypeOptions();
    }

    @ApiOperation(value = "查询同级下是否还有其它相同的字典")
    @GetMapping("/has-some-sibling")
    public Boolean hasSomeSiblingDict(Integer id,@RequestParam(name = "name") String name,@RequestParam(name = "parentId") Integer parentId){
        return dictService.hasSomeSiblingDict(id,name,parentId);
    }

    @ApiOperation(value = "顶级目录是否存在其它相同的字典编码")
    @GetMapping("/has-some-sibling-code")
    public Boolean hasSomeSiblingCode(Integer id,@RequestParam(name = "code")String code){
        return dictService.hasSomeSiblingCode(id,code);
    }
}
